package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.BalanceWarningMapper;
import com.cyy.springbootinit.model.dto.balanceWarning.BalanceWarningQueryRequest;
import com.cyy.springbootinit.model.entity.BalanceWarning;
import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.BalanceWarningVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.BalanceWarningService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 余额预警表服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class BalanceWarningServiceImpl extends ServiceImpl<BalanceWarningMapper, BalanceWarning> implements BalanceWarningService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param balanceWarning
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validBalanceWarning(BalanceWarning balanceWarning, boolean add) {
        ThrowUtils.throwIf(balanceWarning == null, ErrorCode.PARAMS_ERROR);
        //BalanceWarning balanceWarning1=new BalanceWarning();
        // todo 从对象中取值
        Integer warningId = balanceWarning.getWarningId();
        Double thresholdValue = balanceWarning.getThresholdValue();
        String warningType = balanceWarning.getWarningType();

                // 创建数据时，参数不能为空
        if (add) {

            // todo 补充校验规则
            ThrowUtils.throwIf(thresholdValue==null, ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(!StringUtils.isNotBlank(warningType), ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
    }

    /**
     * 获取查询条件
     *
     * @param balanceWarningQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<BalanceWarning> getQueryWrapper(BalanceWarningQueryRequest balanceWarningQueryRequest) {
        QueryWrapper<BalanceWarning> queryWrapper = new QueryWrapper<>();
        if (balanceWarningQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Integer warningId = balanceWarningQueryRequest.getWarningId();
        //Date warningDate = balanceWarningQueryRequest.getWarningDate();
        Double thresholdValue = balanceWarningQueryRequest.getThresholdValue();
        String warningType = balanceWarningQueryRequest.getWarningType();
        String searchText = balanceWarningQueryRequest.getSearchText();
        String sortField = balanceWarningQueryRequest.getSortField();
        String sortOrder = balanceWarningQueryRequest.getSortOrder();
        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }
        // 模糊查询

        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(warningId), "warningId",warningId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(thresholdValue), "thresholdValue", thresholdValue);
        queryWrapper.eq(ObjectUtils.isNotEmpty(warningType), "warningType", warningType);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取余额预警表封装
     *
     * @param balanceWarning
     * @param request
     * @return
     */
    @Override
    public BalanceWarningVO getBalanceWarningVO(BalanceWarning balanceWarning, HttpServletRequest request) {
        // 对象转封装类
        BalanceWarningVO balanceWarningVO = BalanceWarningVO.objToVo(balanceWarning);

        return balanceWarningVO;
    }

    /**
     * 分页获取余额预警表封装
     *
     * @param balanceWarningPage
     * @param request
     * @return
     */
    @Override
    public Page<BalanceWarningVO> getBalanceWarningVOPage(Page<BalanceWarning> balanceWarningPage, HttpServletRequest request) {
        List<BalanceWarning> balanceWarningList = balanceWarningPage.getRecords();
        Page<BalanceWarningVO> balanceWarningVOPage = new Page<>(balanceWarningPage.getCurrent(), balanceWarningPage.getSize(), balanceWarningPage.getTotal());
        if (CollUtil.isEmpty(balanceWarningList)) {
            return balanceWarningVOPage;
        }
        // 对象列表 => 封装对象列表
        List<BalanceWarningVO> balanceWarningVOList = balanceWarningList.stream().map(balanceWarning -> {
            return BalanceWarningVO.objToVo(balanceWarning);
        }).collect(Collectors.toList());
        balanceWarningVOPage.setRecords(balanceWarningVOList);
        return balanceWarningVOPage;
    }

}
